package com.ifunyoung.action;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sun.org.apache.xml.internal.dtm.ref.IncrementalSAXSource_Xerces;

@Controller
public class LoginAction {
	
	private static final Logger logger = LoggerFactory.getLogger(LoginAction.class);

	@RequestMapping("/login")
	public String login(@RequestParam("username") String username, @RequestParam("password") String password){
		// 1.获取登录用户
		Subject subject = SecurityUtils.getSubject();
		
		// 判断用户是否登陆
		if (!subject.isAuthenticated()) {
			// 2.构造token
			UsernamePasswordToken token = new UsernamePasswordToken(username, password);
			token.setRememberMe(true);
			try {
				// 3.进行登陆操作
				subject.login(token);
			} catch (UnknownAccountException e) {
				logger.info("未知用户名");
				e.printStackTrace();
				return "login";
			} catch (IncorrectCredentialsException e) {
				logger.info("密码错误");
				e.printStackTrace();
				return "login";
			}
		}
		return "success";
	}
}
